
var app = getApp()
Page({
	data: {
		accountId:'',
		balance:'',
		getCashNum:'',
		phone:'',
		sendVer:'获取验证码',
		code:'',
		isMask:false
	},
	onLoad: function (options) {
		app.getMemInfo().then(res=>{
			this.setData({
				accountId:res.data.id,
				phone:res.data.username
			})
			this.balance(res.data.id)
		})
	},
	getCashNum(e){
		if (e.detail.value>this.data.balance) {
			app.errorMsg('超出可提现余额')
		}
		this.setData({
			getCashNum:e.detail.value
		})
	},
	getCash(){
		let money = this.data.getCashNum
		let that = this
		if (money!='') {
			if (money>this.data.balance) {
				this.setData({
					isMask:false
				})
				app.errorMsg('超出可提现余额')
			}else if(money<1){
				app.errorMsg('最低提现金额不小于1元')
			}else{
				this.setData({
					isMask:true
				})
			}
		}else{
			app.errorMsg('请输入提现金额')
		}
	},
	//发送验证码
	sendVer(){
		console.log(321)
		if (this.data.sendVer==='获取验证码') {
			let data = {
				accountId:this.data.accountId
			}
			console.log(data)
			app.request('wx_app/withdrawToChange/sendSmsCode',data,'GET').then(res=>{
				console.log(res)
				if (res.code==0) {
					app.errorMsg(res.message)
				}
			})
			this.setData({
				num:120,
				sendVer:'120s后重新发送'
			})
			let time = setInterval(()=>{
				let num = this.data.num
				num --
				this.setData({
					num,
					sendVer:num+'s后重新发送'
				})

				if(num == 0){
					clearInterval(time)
					this.setData({
						sendVer:'发送验证码'
					})
				}
			},1000)
		}else{

		}
	},
	getCode(e){
		this.setData({
			code:e.detail.value
		})
	},
	cancel(){
		this.setData({
			isMask:false,
			sendVer:'获取验证码',
		})
	},
	confirm(){
		wx.showLoading({
			title:'正在提现'
		})
		let data = {
			accountId:this.data.accountId,
			code:this.data.code
		}
		app.request('wx_app/withdrawToChange/checkCode',data,'GET').then(res=>{
			console.log(res)
			if (res.code==1) {
				let data = {
					agentAccountId:this.data.accountId,
					money:this.data.getCashNum
				}
				app.request('wx_app/withdrawToChange',data,'GET').then(res=>{
					console.log(res)
					if (res.code==1) {
						wx.hideLoading()
						this.setData({
							isMask:false
						})
						wx.showToast({
							title:'提现成功',
							icon:'success'
						})
						setTimeout(function(){
							wx.navigateBack({
								delta: 1
							})
						},1000)
					}else{
						this.setData({
							isMask:false
						})
						wx.hideLoading()
						wx.showToast({
							title:'提现失败',
							image:'../../../image/wrong.png'
						})
					}
				})
			}
		})
	},
	balance(accountId){
		let data = {
			accountId
		}
		app.request('wx_app/agent/account/getByid',data,'GET').then(res=>{
			console.log(res)
			this.setData({
				balance:res.data.agent.accountBalance
			})
		})
	}
})